# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
from scrapyProject.spiders.tianya.items import TitleItem, ContentItem, NextPageItem, LinkItem
from scrapyProject.spiders.tianya.tianya_spider import TianyaSpider

import sqlite3
import sys
sys.path.append("..")
import scrapy


class ItemLoadNextPage(object):
    def process_item(self, item, spider):
        if isinstance(item, NextPageItem) == True and isinstance(spider, TianyaSpider) == True:
            yield spider.RequestUrl(url=item["nextPage"], callback=spider.parse)
            return  item

'''
class ItemTxtSave(object):
    def process_item(self, item, spider):
        c = type(item)
        a = isinstance(item, TitleItem)


        if isinstance(item, ContentItem) == True :
            filename = "d:\\hh\\" + item["title"]+".txt"
            with open(filename, 'a+') as f:
                f.write(item["content"])
        return  item

class ItemDataBase(object):

    def open_spider(self, spider):
        self.m_conn = sqlite3.connect('.\\DataBase\\tinaya.db')
        sql = 'delete from ghostData'
        c = self.m_conn.cursor()
        c.execute(sql)
        self.m_conn.commit()
        i=0

    def close_spider(self, spider):
        self.m_conn.close()

    def process_item(self, item, spider):
        #保存数据
        sql = ""
        if isinstance(item, ContentItem):
            sql = "UPDATE ghostData SET content = content||\'" + item["content"] + "\' where id = " + str(item['id'])

        # 插入数据
        elif isinstance(item, TitleItem) == True:
            sql = 'insert into ghostData(title, author, content, id) VALUES (\'' + item["title"]+'\',\'' +item["autor"]+"\'," + "\'\'," +str(item["id"])+")"
        if sql !="":
            c = self.m_conn.cursor()
            c.execute(sql)
            self.m_conn.commit()
        return  item
'''


from scrapyProject.spiders.tianya.tianyaDataBase import tianyaDataBase

class ItemDataBase():
    def open_spider(self, spider):
        self.m_dataHandle = tianyaDataBase()

    def process_item(self, item, spider):
        #保存数据
        if isinstance(item, ContentItem):
            item["id"]=self.m_dataHandle.insertAndUpdateData(item["content"], item["idTitle"],item["idUrl"],item["inxInUrl"])
        # 插入数据
        elif isinstance(item, TitleItem) == True:
            item["id"]=self.m_dataHandle.insertAndUpdateTitle(item["title"], item["url"],item["dataLink"],item["autor"])
        elif isinstance(item, LinkItem) == True:
            item["id"] = self.m_dataHandle.insertAnddUpdateLink(item["idTitle"], item["url"], item["content"])
        return  item

